<?php
$this->setLayout('template' . DS . 'default.phtml');

$this->startBlock('topbar');
echo $this->navigation->toHtml();
$this->endBlock();

$this->startBlock();
echo $this->toolbar->toHtml();
echo $this->form->toHtml();
?>
<script language="javascript" type="text/javascript">
var templates = [];
<?php foreach ($this->pageTemplates as $template): ?>
templates.push({'id':'<?php echo $template->getId() ?>', 'themeId':'<?php echo $template->getPageThemeId() ?>', 'name':'<?php echo $template->getName() ?>'});
<?php endforeach; ?>
function changeTemplate(val){
    var html = '<option value="">--</option>';
    if(val == ''){
        for(var i = 0; i < templates.length; i++){
            if(templates[i].id == '00000000000000000000000000000000'){
                html += '<option value="' + templates[i].id + '">' + templates[i].name + '</option>';
                break;
            }
        }
    }else{
        for(var i = 0; i < templates.length; i++){
            if(templates[i].themeId == val){
                html += '<option value="' + templates[i].id + '">' + templates[i].name + '</option>';
            }
        }
    }
    $('#page_template_id').html(html);
}

$(function(){
    $('#page_theme_id').change(function(){
        changeTemplate($(this).val())
    });
    if($('#selected_template_id').length > 0){
        var templateId = $('#selected_template_id').val();
        changeTemplate($('#page_theme_id').val());
        $('#page_template_id option').each(function(){
            if($(this).attr('value') == templateId){
                $(this).attr('selected', 'selected');
                return false;
            }
        });
    }
});
</script>
<?php
$this->endBlock();